package qc.module.qms.service.exchange;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qc.module.qms.entity.ExchangeCategoryStation;
import qc.module.qms.repository.ExchangeCategoryStationRepository;

import java.util.List;

/**
 * ExchangeCategoryStationService
 *
 * @author QuCheng Tech
 * @since 2024/1/27
 */
@Service
public class ExchangeCategoryStationService {
    private ExchangeCategoryStationRepository repository;

    @Autowired
    public void setExchangeCategoryStationRepository(ExchangeCategoryStationRepository repository) {
        this.repository = repository;
    }

    /***
     * 获取所有交换数据分类与站点关联关系
     *
     * @return java.util.List<qc.module.qms.entity.ExchangeCategoryStation>
     * @author QuCheng Tech
     * @since 2024/1/25
     */
    public List<ExchangeCategoryStation> getAllCategoryStation() {
        LambdaQueryWrapper<ExchangeCategoryStation> wrapper = new LambdaQueryWrapper<>();

        //排序，按站点编码升序
        wrapper.orderByAsc(ExchangeCategoryStation::getStcd);

        //查询数据
        return repository.selectList(wrapper);
    }

    /***
     * 查询指定分类ID集合中站点关联信息
     *
     * @param categoryIds 分类ID集合
     * @return java.util.List<qc.module.qms.dto.exchangeStation.ExchangeStationDto>
     * @author QuCheng Tech
     * @since 2024/1/25
     */
    public List<ExchangeCategoryStation> getCategoryStations(Integer[] categoryIds) {
        //如果传入的分类ID集合为空，返回结果为空
        if (categoryIds != null && categoryIds.length > 0x0) {
            LambdaQueryWrapper<ExchangeCategoryStation> wrapper = new LambdaQueryWrapper<>();
            //优化查询条件：如果站分类ID集合只有1个使用eq条件
            if (categoryIds.length == 0x1)
                wrapper.eq(ExchangeCategoryStation::getCid, categoryIds[0x0]);
            else
                wrapper.in(ExchangeCategoryStation::getCid, categoryIds);

            //排序，按站点编码升序
            wrapper.orderByAsc(ExchangeCategoryStation::getStcd);

            //查询数据
            return repository.selectList(wrapper);
        }

        return null;
    }

    /**
     * 根据站点编码删除站点与分类的关联关系
     *
     * @param stcd 站点编码
     * @author QcCheng Tech
     * @since 2024/4/16
     */
    public void deleteByStcd(String stcd) {
        LambdaQueryWrapper<ExchangeCategoryStation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ExchangeCategoryStation::getStcd, stcd);
        repository.delete(wrapper);
    }
    
    /**
     * 根据分类ID删除分类与站点的关联关系
     *
     * @param cid 分类ID
     * @author QcCheng Tech
     * @since 2024/4/16
     */
    public void deleteByCid(Integer cid) {
        LambdaQueryWrapper<ExchangeCategoryStation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ExchangeCategoryStation::getCid, cid);
        repository.delete(wrapper);
    }

    /**
     * 添加指定分类与站点关联关系及交换方向
     *
     * @param cid 分类ID
     * @param stcds 站点编码集合
     * @param direction 交换方向
     * @author QuCheng Tech
     * @since 2024/4/17
     */
    public void addStationsByCid(int cid, List<String> stcds,int direction) {
        if (CollectionUtils.isEmpty(stcds)){
            //站点编码集合为空则清空指定数据分类ID已有的站点关联关系
            deleteByCid(cid);
        }else{
            //站点编码集合不为空：先清空指定分类原有站点关联关系，在逐个添加新的关联关系
            deleteByCid(cid);
            stcds.stream().forEach(stcd -> {
                ExchangeCategoryStation categoryStation = new ExchangeCategoryStation();
                categoryStation.setCid(cid);
                categoryStation.setStcd(stcd);
                categoryStation.setDirection(direction);
                repository.insert(categoryStation);
            });
        }
    }

    /**
     * 修改指定分类与站点的交换方向
     *
     * @param cid       分类ID
     * @param stcds     站点编码集合
     * @param direction 交换方向
     * @author QuCheng Tech
     * @since 2024/4/17
     */
    public void updateDirection(int cid, List<String> stcds, int direction) {
        if (CollectionUtils.isNotEmpty(stcds)) {
            stcds.stream().forEach(stcd -> {
                LambdaUpdateWrapper<ExchangeCategoryStation> wrapper = new LambdaUpdateWrapper<>();
                wrapper.eq(ExchangeCategoryStation::getCid, cid);
                wrapper.eq(ExchangeCategoryStation::getStcd, stcd);
                wrapper.set(ExchangeCategoryStation::getDirection, direction);
                repository.update(null, wrapper);
            });
        }
    }

    /**
     * 判断关联关系是否存在
     *
     * @param cid 分类ID
     * @param stcd 站点编码
     * @return boolean
     * @author QuCheng Tech
     * @since 2024/4/17
     */
    public boolean hasExist(int cid, String stcd) {
        LambdaQueryWrapper<ExchangeCategoryStation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ExchangeCategoryStation::getCid, cid);
        wrapper.eq(ExchangeCategoryStation::getStcd, stcd);

        ExchangeCategoryStation en = repository.selectOne(wrapper);
        if (en != null)
            return true;

        return false;
    }
}
